//
//  OMOrderCancelSubmitViewController.m
//  OMENGO_NEW
//
//  Created by 梁尚嘉 on 2017/5/15.
//  Copyright © 2017年 Kamfat. All rights reserved.
//

#import "OMOrderCancelSubmitViewController.h"
#import "OMCancelSubmitTableCell.h"

@interface OMOrderCancelSubmitViewController ()

@property (nonatomic, strong) OMCancelSubmitTableCell *tableCell;
@property (nonatomic, strong) NSArray *reasons;

@end

@implementation OMOrderCancelSubmitViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    [self setupUI];
    [self getReasons];

}

- (void)reloadDataFromNetWork
{
    [self getReasons];
}

- (void)getReasons
{
    [self startAnimating:YES];
    [KTHttpUtils GET:@"member/order/reason.jhtml"
          completion:^(id responseObject, BOOL isSuccess, NSString *message) {
              if (isSuccess) {
                  [self startAnimating:NO];
                  self.reasons = responseObject[OMDatumKey];
                  [self.tableCell configureCellWithObj:self.reasons];
              }else {
                  [self showNetWorkError];
              }
          }];
}

- (void)setupUI
{

    self.title = @"取消成功";
    self.tableView.tableHeaderView = self.header;
    self.tableView.separatorStyle = UITableViewCellSeparatorStyleSingleLine;
    self.loading = true;

    OMCancelSubmitTableCell *cell = [OMCancelSubmitTableCell new];
    _tableCell = cell;
    [self.tableView reloadData];
    @weakify(self);
    [RACObserve(self.tableCell, selectedMessage) subscribeNext:^(id x) {
        @strongify(self);
        self.confirmButton.enabled = self.tableCell.selectedMessage.length;
    }];

}

- (UIView *)header
{
    UIView *header = [UIView new];
    header.frame = CGRectMake(0, 0, SM_SCREEN_WIDTH, SM_SIZE(500));
    UIImageView *iconImageView = [[UIImageView alloc]initWithImage:[UIImage st_imageNamed:@"ic_order_canceled@2x" type:@"png"]];
    [header addSubview:iconImageView];
    [iconImageView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.top.equalTo(@(SM_SIZE(100)));
        make.centerX.equalTo(header.mas_centerX);
    }];

    UILabel *messageLabel = [UILabel new];
    messageLabel.font = SM_CBFont(50);
    messageLabel.textColor = SM_blackColor;
    messageLabel.text = @"取消成功";
    [header addSubview:messageLabel];
    [messageLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.centerX.equalTo(iconImageView.mas_centerX);
        make.top.equalTo(iconImageView.mas_bottom).offset(SM_SIZE(50));
    }];

    UILabel *statusLabel = [UILabel new];
    statusLabel.font = SM_CFont(28);
    statusLabel.textColor = SM_grayColor;
    statusLabel.text = @"小主，请赐我一个取消理由吧，这样我才能做的更好!";
    statusLabel.numberOfLines = 2;
    statusLabel.textAlignment = NSTextAlignmentCenter;
    [header addSubview:statusLabel];
    [statusLabel mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.equalTo(@(SM_SIZE(30)));
        make.bottom.equalTo(@(SM_SIZE(-30)));
        make.right.equalTo(@(SM_SIZE(-30)));
    }];
    return header;
}

#pragma mark - Getters



#pragma mark - table view
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section
{
    if (_tableCell) {
        return 1;
    }
    return 0;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return _tableCell;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath
{
    return [OMCancelSubmitTableCell cellHeight];
}



/**
 取消订单原因的提交

 @param sender 按钮
 */
- (void)submitAction:(UIButton *)sender
{
    self.confirmButton.enabled = NO;
    [self.task cancel];
    self.task = [KTHttpUtils POST:@"member/order/submit.jhtml"
                       parameters:@{@"orderId":self.order.id,@"content":self.tableCell.selectedMessage}
                         progress:nil
                       completion:^(id responseObject, BOOL isSuccess, NSString *message) {
                           if (isSuccess) {
                               NaviDismiss(nil);
                           }else {
                               self.confirmButton.enabled = YES;
                               [OMHUD HUDInfo:message?:@"取消原因提交出错"];
                           }
                       }];
}
@end
